package action.client;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import service.securite.DroitsAcces;

import action.general.NotreServlet;
import bdd.BddClientSelectRequest;
import bdd.BddCommandeSelectRequest;
import bean.commande.Commande;



public class MonCompteClientAction extends NotreServlet {
    
	static final long serialVersionUID = 1L;
    
    public static boolean aLesDroits(HttpServletRequest request) {
    	if (!DroitsAcces.isConnecte(request))
    		return false;
    	
    	return true;
    }
   
    public static String datetostring(Date sDate) throws Exception 
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(sDate);
    }
    

	public ActionForward execute( ActionMapping pMapping, ActionForm pForm, HttpServletRequest pRequest, HttpServletResponse pResponse)
	{
		ActionErrors errors = new ActionErrors();
    	if (!MonCompteClientAction.aLesDroits(pRequest)) {
    		return pMapping.findForward("error");
    	}
    	
    	ArrayList<Commande> commandes = null;
    	HttpSession session = pRequest.getSession();
    	String idCompte = (String)session.getAttribute("id_compte");
    	String compteService = null;
    	
		try {
			commandes = BddCommandeSelectRequest.getCommandesClient(Integer.parseInt(idCompte));
	    	session.setAttribute("listeCommandes", commandes);
			compteService = BddClientSelectRequest.getServiceCompte(idCompte);
		} catch (Exception e1) {
			e1.printStackTrace();
			return pMapping.findForward("error");
		}
   	
    	String civilite_recuperee = "";
    	String nomCompte_recupere  = "";
    	String prenomCompte_recupere  = "";
    	String serviceCompte_recupere  = "";
    	String tel1Compte_recupere  = "";
    	String tel2Compte_recupere  = ""; 
    	String voieAdresse_recuperee  = ""; 
    	String complementAdresse_recupere = "";
    	String codePostaAdresse_recupere  = "";
    	String villeAdresse_recupere  = "";
    	String paysAdresse_recupere  = "";

		try {
			ArrayList<String> infoCompte = new ArrayList<String>();
			if (compteService != null){
				infoCompte = BddClientSelectRequest.recupereInformationCompteClient(idCompte);
				civilite_recuperee = infoCompte.get(0);
		    	nomCompte_recupere = infoCompte.get(1);
		    	prenomCompte_recupere = infoCompte.get(2);
		    	serviceCompte_recupere = infoCompte.get(3);
		    	tel1Compte_recupere = infoCompte.get(4);
		    	tel2Compte_recupere = infoCompte.get(5);
		    	voieAdresse_recuperee = infoCompte.get(6);
		    	complementAdresse_recupere = infoCompte.get(7);
		    	codePostaAdresse_recupere = infoCompte.get(8);
		    	villeAdresse_recupere = infoCompte.get(9);
		    	paysAdresse_recupere = infoCompte.get(10);}
			else {
				 infoCompte = BddClientSelectRequest.recupereInformationSansCompteClient(idCompte);
				 civilite_recuperee = infoCompte.get(0);
		    	 nomCompte_recupere = infoCompte.get(1);
		    	 prenomCompte_recupere = infoCompte.get(2);
		    	 tel1Compte_recupere = infoCompte.get(3);
		    	 tel2Compte_recupere = infoCompte.get(4);
		    	 voieAdresse_recuperee = infoCompte.get(5);
		    	 complementAdresse_recupere = infoCompte.get(6);
		    	 codePostaAdresse_recupere = infoCompte.get(7);
		    	 villeAdresse_recupere = infoCompte.get(8);
		    	 paysAdresse_recupere = infoCompte.get(9);
		    	}
			
	    	
	    	Date dateNaissance_recuperee = BddClientSelectRequest.recupereDateClient(idCompte);
	    	DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	    	String madate = format.format(dateNaissance_recuperee);
   	     	String ddn[] = madate.split("/");
   	     	String jour = ddn[0];
   	     	String mois = ddn[1];
   	     	String annee = ddn[2];
	    	pRequest.setAttribute("civiliteClient", civilite_recuperee);
	    	pRequest.setAttribute("nomClient", nomCompte_recupere);
	    	pRequest.setAttribute("prenomClient", prenomCompte_recupere);
	    	pRequest.setAttribute("serviceCompte", serviceCompte_recupere);
	    	pRequest.setAttribute("tel1Compte", tel1Compte_recupere);
	    	pRequest.setAttribute("tel2Compte", tel2Compte_recupere);
	    	pRequest.setAttribute("voieAdresse", voieAdresse_recuperee);
	    	pRequest.setAttribute("complementAdresse", complementAdresse_recupere);
	    	pRequest.setAttribute("codePostaAdresse", codePostaAdresse_recupere);
	    	pRequest.setAttribute("villeAdresse", villeAdresse_recupere);
	    	pRequest.setAttribute("paysAdresse", paysAdresse_recupere);
	    	pRequest.setAttribute("jour", jour);
	    	pRequest.setAttribute("mois", mois);
	    	pRequest.setAttribute("annee", annee);
	    	
		} catch (Exception e) {
			e.printStackTrace();
			errors.add("errors",new ActionMessage("errors.exception"));
			saveErrors(pRequest,errors); 
			return (new ActionForward(pMapping.getInput()));
		}
    		
		return pMapping.findForward("success");
	}
	
}